package com.ucode.dcse.option;
import java.util.List;
import org.springframework.stereotype.Component;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.ucode.springboot.starter.canal.option.dml.InsertOption;

/**
 * 需要自己实现的新增处理机制
 * @author: liliang
 * @date: 2020年3月1日 下午10:16:03
 */
@Component
public class RealInsertOptoin extends InsertOption {
	
	
	@Override
	public void doOption(String destination, String schemaName, String tableName, CanalEntry.RowChange rowChange) {
		System.out.println("======================接口方式（新增数据操作）==========================");
		List<CanalEntry.RowData> rowDatasList = rowChange.getRowDatasList();
		for (CanalEntry.RowData rowData : rowDatasList) {
			
			String sql = "use " + schemaName + ";\n";
			StringBuffer colums = new StringBuffer();
			StringBuffer values = new StringBuffer();
			rowData.getAfterColumnsList().forEach((c) -> {
				colums.append(c.getName() + ",");
				values.append("'" + c.getValue() + "',");
			});
			
			
			sql += "INSERT INTO " + tableName + "(" + colums.substring(0, colums.length() - 1) + ") VALUES(" + values.substring(0, values.length() - 1) + ");";
			System.out.println(sql);
		}
		System.out.println("\n======================================================");
		
	}
}